<template>
  <a-drawer
    :title="title"
    width="40%"
    :closable="false"
    :maskClosable="false"
    :visible="visible"
    :body-style="{ paddingBottom: '80px'}"
    @close="onClose"
  >
    <a-form-model 
      ref="ruleForm"
      :model="form" 
      :rules="rules"
      :label-col="labelCol" 
      :wrapper-col="wrapperCol" 
      :key="visible"
    >
      <a-form-model-item label="图片" required>
        <a-upload
          list-type="picture-card"
          :file-list="fileList"
          :remove="hanldeImgRemove"
          @preview="handlePreview"
          :customRequest="handleUploadChange"
          class="upload-half"
          accept="image/*,video/*"
        >
          <div class="custom-btn" v-if="fileList.length<1">
            <a-icon type="ant-cloud" style="font-size: 26px" />
            <div class="ant-upload-text">上传图片(1920*768)</div>
          </div>
        </a-upload>
      </a-form-model-item>
      <a-form-model-item label="排序" prop="sortNum">
        <a-input v-model="form.sortNum" placeholder="排序" />
      </a-form-model-item>
      <!-- <a-form-model-item label="手机端图片" required>
        <a-upload
          list-type="picture-card"
          :file-list="fileList1"
          :remove="hanldeImgRemove1"
          @preview="handlePreview"
          :customRequest="handleUploadChange1"
          class="upload-half"
          accept="image/*"
        >
          <div class="custom-btn" v-if="fileList1.length<1">
            <a-icon type="ant-cloud" style="font-size: 26px" />
            <div class="ant-upload-text">手机端图片</div>
          </div>
        </a-upload>
      </a-form-model-item> -->
    </a-form-model>
    <div
      :style="{
        position: 'absolute',
        right: 0,
        bottom: 0,
        width: '100%',
        borderTop: '1px solid #e9e9e9',
        padding: '10px 16px',
        background: '#fff',
        textAlign: 'right',
        zIndex: 99999,
      }"
    >
      <a-button :style="{ marginRight: '8px' }" @click="onClose"> 关闭 </a-button>
      <a-button type="primary" @click="onSubmit"> 提交 </a-button>
    </div>
    <a-modal :visible="previewVisible" :footer="null" @cancel="previewVisible=false" width="60%">
      <img alt="example" style="width: 100%" :src="previewImage" v-if="previewImage.indexOf('.mp4') == -1" />
      <video :src="previewImage" v-else controls style="width: 100%"></video>
    </a-modal>
  </a-drawer>
</template>

<script>

function getBase64(file) {
  return new Promise((resolve, reject) => {
    const reader = new FileReader()
    reader.readAsDataURL(file)
    reader.onload = () => resolve(reader.result)
    reader.onerror = error => reject(error)
  })
}
import { updateHomePages, addHomePages } from '@/api/manage'
import { showMessage } from '@/utils/mixins'
import { uploadFile, deleteFile } from '@/api/common'
import { getFileName } from '@/utils/util'

export default {
  mixins: [showMessage],
  props: {
    parent: {
      type: Object,
      default: () => ({})
    }
  },
  data() {
    return {
      title: '',
      visible: false,
      labelCol: {span: 3},
      wrapperCol: {span: 21},
      form: {
        imageUrl: undefined,
        sortNum: undefined
      },
      rules: {},
      fileList: [],
      previewVisible: false,
      previewImage: '',
      id: undefined,
      status: undefined
    }
  },
  methods: {
    //上传手机端图片
    // handleUploadChange1(file) {
    //   file = file.file
    //   const _this = this
    //   const formdata = new FormData()
    //   formdata.append('file', file)
    //   uploadFile(formdata).then(res => {
    //     if(res.code == '0') {
    //       file.url = process.env.VUE_APP_API_ORIGIN+res.data
    //       file.uid = Math.random()
    //       file.filename = getFileName( res.data )
    //       _this.fileList1.push(file)
    //       _this.form.imageUrl = res.data
    //     }else {
    //       _this.$message.error(res.message)
    //     }
    //   })
    // },
    //自定义上传图片
    handleUploadChange(file) {
      file = file.file
      const _this = this
      const formdata = new FormData()
      formdata.append('file', file)
      uploadFile(formdata).then(res => {
        if(res.code == '0') {
          file.url = process.env.VUE_APP_API_ORIGIN+res.data
          file.uid = Math.random()
          file.filename = getFileName( res.data )
          _this.fileList.push(file)
          _this.form.imageUrl = res.data
        }else {
          _this.$message.error(res.message)
        }
      })
    },
    //删除图片
    hanldeImgRemove(file) {
     deleteFile({filePath: file.url}).then(res => {
        if(res.code == '0') {
          this.showMessage(res,() => {
            this.fileList = []
          })
        }
      })
    },
    // 预览
    async handlePreview(file) {
      if (!file.url && !file.preview ) {
        file.preview = await getBase64(file.originFileObj)
      }
      this.previewImage = file.url || file.preview
      this.previewVisible = true
    },
    // 提交
    onSubmit () {
      if(!this.form.imageUrl) {
        return this.$message.warning('图片不能为空')
      }
      if(this.status == 1) {
        addHomePages(this.form).then(res => {
          this.showMessage(res,() => {
            this.onClose()
            this.parent.getList()
          })
        })
      }else {
        updateHomePages({id:this.id,...this.form}).then(res => {
          if(res.code == '0') {
            this.showMessage(res,() => {
              this.onClose()
              this.parent.getList()
            })
          }
        })
      }
      
    },
    // 关闭
    onClose() {
      this.visible = false
      this.id = undefined
      this.fileList = []
      this.status = undefined
      for(const key in this.form) {
        this.form[key] = undefined
      }
    }
  }
}
</script>

<style lang="less" scoped>
.plus-icon,
.minus-icon {
  font-size: 20px;
  margin-right: 6px;
  float: right;
  color: #F5222D;
  margin-top: 10px;
}
.minus-icon {
  color: #1890ff;
}
</style>